﻿using MediatR;
using ProcurementManagement.Applcation.Command.PurchaseReturnsCommand;
using ProcurementManagement.Domain.ProcurementDomain;
using ProcurementManagement.Infrstructre.Interface;

namespace ProcurementManagement.Applcation.CommandHanlers.PurchaseReturnsCommandHandler
{
    /// <summary>
    /// 实退采购款列表中介者
    /// </summary>
    public class PurchaseReturnsSelectCommandHandler:IRequestHandler<PurchaseReturnsSelectCommand, PageDirectRefund>
    {
        private readonly lnterfaceDirectRefundRepository _directRefundRepository;

        public PurchaseReturnsSelectCommandHandler(lnterfaceDirectRefundRepository directRefundRepository)
        {
            _directRefundRepository = directRefundRepository;
        }

        public async Task<PageDirectRefund> Handle(PurchaseReturnsSelectCommand request, CancellationToken cancellationToken)
        {
            var res = (await _directRefundRepository.GetAll()).Where(x => x.IsDel == false).ToList();
            if (!string.IsNullOrEmpty(request.VName))
            {
                res = res.Where(x => x.VName==request.VName).ToList();
            }
            if (request.BenPrice != null)
            {
                res = res.Where(x => x.BenPrice >= request.BenPrice).ToList();
            }
            if (request.SBenPrice != null)
            {
                res = res.Where(x => x.BenPrice <= request.SBenPrice).ToList();
            }
            if (!string.IsNullOrEmpty(request.BenDate))
            {
                res = res.Where(x => x.BenDate >=Convert.ToDateTime(request.SBenPrice)).ToList();
            }
            if (!string.IsNullOrEmpty(request.SBenDate))
            {
                res = res.Where(x => x.BenDate <= Convert.ToDateTime(request.SBenDate)).ToList();
            }
            if (!string.IsNullOrEmpty(request.PReturnsPersonnel))
            {
                res = res.Where(x => x.PReturnsPersonnel == request.PReturnsPersonnel).ToList();
            }
            if (!string.IsNullOrEmpty(request.YingHang))
            {
                res = res.Where(x => x.YingHang == request.YingHang).ToList();
            }
            if (!string.IsNullOrEmpty(request.PurchaseReturnsCode))
            {
                res = res.Where(x => x.PurchaseReturnsCode == request.PurchaseReturnsCode).ToList();
            }
            if (!string.IsNullOrEmpty(request.PurchaseReturnsTopic))
            {
                res = res.Where(x => x.PurchaseReturnsTopic == request.PurchaseReturnsTopic).ToList();
            }
            PageDirectRefund page = new PageDirectRefund();
            page.SumCount = res.Count();
            page.PageLast = (int)Math.Ceiling(page.SumCount * 1.0 / request.PageSize);
            page.list = res.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
            return page;


        }
    }
}
